window.range = (l, r) => {
  const len = r - l + 1
  const arr = Array(len)

  for (let i = 0; i < len; i++) {
    arr[i] = i + l
  }

  return arr
}

window.rand = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1) + min)
}

window.sleep = (time) => {
  return new Promise(next => setTimeout(next, time))
}

window.randColor = (a) => {
  return `rgba(${rand(0, 255)}, ${rand(0, 255)}, ${rand(0, 255)}, ${a || 1})`
}

window.clone = (o) => {
  return JSON.parse(JSON.stringify(0))
}

window.d2a = (deg) => {
  return deg / 180 * Math.PI
}

window.a2d = (angle) => {
  return angle / Math.PI * 180
}

Array.prototype.unique = function() {
  return Array.from(new Set(this))
}

Array.prototype.first = function() {
  return this[0]
}

Array.prototype.last = function() {
  return this[this.length - 1]
}

Array.prototype.swap = function(a, b) {
  const t = this[a]
  this[a] = this[b]
  this[b] = t
}

Array.prototype.shuffle = function() {
  for (let i = this.length - 1; i > 0; i--) {
    this.swap(i, Math.floor(Math.random() * (i + 1)))
  }
  return this
}

Array.prototype.sum = function() {
  let total = 0
  for (let i = 0, len = this.length; i < len; i++) {
    total += this[i]
  }
  return total
}

Array.prototype.min = function() {
  if (!this.length) return 0;
  let iMin = Infinity
  for (let i = this.length - 1; i > -1; i--) {
    iMin = Math.min(iMin, this[i])
  }
  return iMin
}

Array.prototype.max = function() {
  if (!this.length) return 0;
  let iMax = -Infinity
  for (let i = this.length - 1; i > -1; i--) {
    iMax = Math.max(iMax, this[i])
  }
  return iMax
}

Date.prototype.format = function(format) {
  const o = {
    y: this.getFullYear(),
    m: this.getMonth() + 1,
    d: this.getDate(),
    h: this.getHours(),
    i: this.getMinutes(),
    s: this.getSeconds(),
  }

  return (format || 'y-m-d h:i:s').replace(/y|m|d|h|i|s/g, (k) => {
    const n = o[k]
    return n < 10 ? '0' + n : n
  })
}